package com.example.administrator_kunker.mjj_pro.util;

import android.support.annotation.NonNull;
import com.vilyever.socketclient.SocketClient;
import com.vilyever.socketclient.helper.SocketClientDelegate;
import com.vilyever.socketclient.helper.SocketClientReceivingDelegate;
import com.vilyever.socketclient.helper.SocketResponsePacket;
import com.vilyever.socketclient.util.CharsetUtil;

public class AndroidClient {

    private static volatile AndroidClient instance ;
    private SocketClient client;
    private static  String ip , port;
    private static  int timeOut = 10 * 1000 , heartBeatInterval = 2 * 1000;
    //超时  10s   心跳包  2s
    private static String heartArea = "heartBeat";  //心跳包内容

    //加载类的时候就初始化
    /*  不可用
    static {
        instance = new AndroidClient();
    }*/
    private AndroidClient() {
        initNet();
    }

    //线程安全
    public static AndroidClient getInstance(String ip , String port){
        setIp(ip);
        setPort(port);
        if(instance == null){
            synchronized (AndroidClient.class){
                if(instance == null){
                    instance = new AndroidClient();
                }
            }
        }
        return instance;
    }

    //线程安全
    public static AndroidClient getInstance(String ip , String port,int timeOut , int heartBeatInterval , String heartArea){
        setIp(ip);
        setPort(port);
        setTimeOut(timeOut);
        setHeartArea(heartArea);
        setHeartBeatInterval(heartBeatInterval);
        if(instance == null){
            synchronized (AndroidClient.class){
                if(instance == null){
                    instance = new AndroidClient();
                }
            }
        }
        return instance;
    }

    //初始化
    private void initNet(){
        client = new SocketClient();
        client.getAddress().setRemoteIP(ip);
        client.getAddress().setRemotePort(port);
        client.getAddress().setConnectionTimeout(timeOut);  //超时时间
        client.setCharsetName(CharsetUtil.UTF_8);
        //设置心跳包内容
        client.getHeartBeatHelper().setDefaultSendData(CharsetUtil.stringToData(heartArea,CharsetUtil.UTF_8));
        client.getHeartBeatHelper().setHeartBeatInterval(heartBeatInterval) ; //设置心跳包发送间隔时长，单位毫秒
        client.getHeartBeatHelper().setSendHeartBeatEnabled(true);  //设置允许发送心跳包

    }

    //连接
    public void connect(){
        client.connect();
    }

    //发送
    public void sendCmd(String msg){
        client.sendString(msg);
    }

    public void addListener(SocketClientDelegate state,SocketClientReceivingDelegate receive){
        client.registerSocketClientDelegate(state);
        client.registerSocketClientReceiveDelegate(receive);
    }

    //销毁
    public void destory(){
        client.disconnect();
        //client = null;
    }

    public static String getIp() {
        return ip;
    }

    public static void setIp(String ip) {
        AndroidClient.ip = ip;
    }

    public static String getPort() {
        return port;
    }

    public static void setPort(String port) {
        AndroidClient.port = port;
    }

    public static int getTimeOut() {
        return timeOut;
    }

    public static void setTimeOut(int timeOut) {
        AndroidClient.timeOut = timeOut;
    }

    public static int getHeartBeatInterval() {
        return heartBeatInterval;
    }

    public static void setHeartBeatInterval(int heartBeatInterval) {
        AndroidClient.heartBeatInterval = heartBeatInterval;
    }

    public static String getHeartArea() {
        return heartArea;
    }

    public static void setHeartArea(String heartArea) {
        AndroidClient.heartArea = heartArea;
    }


    public boolean state() {
        return client.isConnected();
    }
}
